﻿using UnityEngine;
using System.Collections;
using UnityEditor;

public class AnimationClone : EditorWindow
{
    readonly string STR_INTRODUCE = "将模型的所有动画克隆出来.\n" + "1.选择一个有动画模型";

    readonly string STR_OK = "克隆动画";

    private GameObject animationModel;

    [MenuItem("Level4/AnimationTool/AnimationClone")]
    static void Init()
    {
        EditorWindow.GetWindow<AnimationClone>();
    }

    void OnGUI()
    {
        GUILayout.Box(STR_INTRODUCE);
        animationModel = EditorGUILayout.ObjectField(Selection.activeObject, typeof(Object),true) as GameObject;
        if (animationModel != null && animationModel.animation != null)
        {
            if (GUILayout.Button(STR_OK))
            {
                CloneAnimations();
            }
        }
    }

    void OnSelectionChange()
    {
        Repaint();
    }
    /// <summary>
    /// 客户 动画模型的所有动画.
    /// </summary>
    void CloneAnimations()
    {
        AnimationClip[] clips = AnimationUtility.GetAnimationClips(animationModel);
        string dir = AssetDatabase.GetAssetPath(animationModel);
        string folderName = animationModel.name + "_Anims";
        string newPath = AssetDatabase.GUIDToAssetPath(AssetDatabase.CreateFolder(dir, folderName));
       
        foreach (AnimationClip clip in clips)
        {
            AssetDatabase.CreateAsset(clip, newPath + "/" + clip.name);
        }
        AssetDatabase.Refresh();
    }
}
